#include <stdio.h>

#define MAX_TERMS 1000 /* maximum number of terms */

typedef struct {
	int row;
	int col;
	int value;
} term;

term a[MAX_TERMS];
term b[MAX_TERMS];
term product[MAX_TERMS];

void readMatrix(term t[]) {
	int r, c, v, n = 0;
	while (1) {
		scanf("%d,%d,%d", &r, &c, &v);
		if (r == -1 && c == -1 && v == -1)
			break;
		t[n].row = r;
		t[n].col = c;
		t[n++].value = v;
	}
	t[n].row = -1; // Mark the end of the matrix
}

int main() {
	int a_terms = 0, b_terms = 0, p_terms = 0;

	// Read matrices A and B
	readMatrix(a);
	readMatrix(b);

	// Perform multiplication
	for (int i = 0; a[i].row != -1; ++i) {
		for (int j = 0; b[j].row != -1; ++j) {
			if (a[i].col == b[j].row) {
				int prod_val = a[i].value * b[j].value;
				// Check if the product term already exists
				int exists = 0;
				for (int k = 0; k < p_terms; ++k) {
					if (product[k].row == a[i].row && product[k].col == b[j].col) {
						product[k].value += prod_val;
						exists = 1;
						break;
					}
				}
				if (!exists) {
					product[p_terms].row = a[i].row;
					product[p_terms].col = b[j].col;
					product[p_terms++].value = prod_val;
				}
			}
		}
	}

	if (p_terms == 0) {
		printf("No non-zero number exists!\n");
	} else {
		// Output the product matrix
		for (int i = 0; i < p_terms; ++i) {
			printf("%d,%d,%d\n", product[i].row, product[i].col, product[i].value);
		}
	}

	return 0;
}
